import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getwidget/getwidget.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sz_metro/page/mine/settings/userInfo.dart';
import 'package:bruno/bruno.dart';
import 'package:sz_metro/utils/cache.dart';
import 'package:sz_metro/utils/platform.dart';

class SettingsContent extends StatefulWidget {
  const SettingsContent({Key? key}) : super(key: key);

  @override
  State<SettingsContent> createState() => _SettingsContentState();
}

class _SettingsContentState extends State<SettingsContent> {
  late TextEditingController searchController = TextEditingController();

  List<BrnCommonActionSheetItem> actions = [
    BrnCommonActionSheetItem(
      '确定清除缓存',
      actionStyle: BrnCommonActionSheetItemStyle.normal,
    ),
    BrnCommonActionSheetItem(
      '清除',
      titleStyle:
          const TextStyle(color: Colors.red, fontWeight: FontWeight.bold),
      actionStyle: BrnCommonActionSheetItemStyle.alert,
    ),
  ];

  String cacheSting = '0';

  @override
  void initState() {
    // 获取缓存
    if (PlatformUtils.isWeb == false){
      getAppCache();// path_provider 不支持web平台
    }
    super.initState();
  }

  @override
  void dispose() async {
    searchController.dispose();
    super.dispose();
  }

  // 获取缓存的大小
  void getAppCache() {
    CacheUtil.loadApplicationCache().then((value) {
      // 获取到缓存大小
      String _cacheSting = CacheUtil.formatSize(value);
      if (mounted) {
        print('获取到缓存大小' + _cacheSting.toString());
        setState(() {
          cacheSting = _cacheSting;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: GFAppBar(title: const Text("设置"), backgroundColor: Colors.green),
      body: ListView(
        children: [
          GFCard(
            boxFit: BoxFit.cover,
            content: Column(children: [
              GFListTile(
                icon: const Icon(
                  Icons.arrow_right,
                  color: Colors.grey,
                ),
                title: const Text('用户资料'),
                onTap: () {
                  Get.to(() => const UserInfoContent());
                },
              ),
              const Divider(
                height: 1,
                color: Colors.grey,
                indent: 25,
              ),
              GFListTile(
                icon: const Icon(
                  Icons.arrow_right,
                  color: Colors.grey,
                ),
                title: const Text('账户安全'),
                onTap: () {},
              ),
              const Divider(
                height: 1,
                color: Colors.grey,
                indent: 25,
              ),
              const GFListTile(
                icon: Icon(
                  Icons.arrow_right,
                  color: Colors.grey,
                ),
                title: Text('系统权限'),
              ),
            ]),
          ),
          Offstage(
            offstage: PlatformUtils.isWeb == true,
            child: GFCard(
              boxFit: BoxFit.cover,
              content: Column(children: [
                GestureDetector(
                  onTap: () async {
                    List? res = await showModalBottomSheet(
                        context: context,
                        backgroundColor: Colors.transparent,
                        builder: (BuildContext context) {
                          return BrnCommonActionSheet(
                            actions: actions,
                            clickCallBack: (
                              int index,
                              BrnCommonActionSheetItem actionEle,
                            ) {
                              String title = actionEle.title;
                              // BrnToast.show(
                              //     "title: $title, index: $index", context);
                            },
                          );
                        });
                    // ignore: avoid_print
                    if (res != null && res[0] == 1) {
                      // 获取缓存
                      Directory directory =
                          await getApplicationDocumentsDirectory();
                      //清楚缓存并重新获取缓存大小
                      CacheUtil.delDir(directory)
                          .then((value) => getAppCache());
                      BrnToast.show(
                        "成功清楚缓存",
                        context,
                        duration: BrnDuration.short,
                      );
                    } else {
                      print(res);
                    }
                  },
                  child: GFListTile(
                    title: const Text('清除缓存'),
                    icon: Row(
                      children: [
                        Text(cacheSting),
                        const Icon(Icons.arrow_right, color: Colors.grey),
                      ],
                    ),
                  ),
                ),
                const Divider(
                  height: 1,
                  color: Colors.grey,
                  indent: 25,
                ),
                const GFListTile(
                  icon: Icon(Icons.arrow_right, color: Colors.grey),
                  title: Text('关于我们'),
                ),
              ]),
            ),
          ),
          GFButton(
            color: GFColors.WHITE,
            onPressed: () {},
            child: const Text(
              "退出登录",
              style: TextStyle(color: Colors.black),
            ),
            fullWidthButton: true,
          )
        ],
      ),
    );
  }
}
